document.onpaste = function (e) {
	var items = e.clipboardData.items;
    var files = [];
    for( var i = 0, len = items.length; i < len; ++i ) {
        var item = items[i];
        if( item.kind === "file" ) {
            submitFileForm(item.getAsFile(), "paste");
			return false;
        }
    }

};

function submitFileForm(file, type) {
	try{
		var extension = file.type.match(/\/([a-z0-9]+)/i)[1].toLowerCase();
		var formData = new FormData();
		formData.append('file', file, "image_file");
		formData.append('extension', extension );
		formData.append("mimetype", file.type );
		formData.append('submission-type', type);
		var xhr = new XMLHttpRequest();
		xhr.responseType = "text";
		xhr.open('POST', '/libraries/kcfinder/clipboard.php');
		xhr.onload = function () {
			try{
				if (xhr.status == 200) {
					serverResponse = JSON.parse(xhr.responseText);
					if(serverResponse.result !=undefined &&  serverResponse.result>=0){
						var img = new Image();
						img.src = serverResponse.imgpath;
						//document.body.appendChild(img);
						for(var instanceName in window.parent.CKEDITOR.instances) {
						   var editor = window.parent.CKEDITOR.instances[instanceName];//.insertHtml(img);
						   editor.insertHtml('<img src="'+serverResponse.imgpath+'"/>')
						}
						//window.parent.CKEDITOR.currentInstance.getIndex().insertHTML(img);
					}else{
						window.parent.WarningMsg(serverResponse.message);
						//alert(serverResponse.message)
					}
				}
			}catch(e){
				alert(e.message)
			}
		};
		xhr.send(formData);
	}catch(e){
		alert(e.message)
	}
}